<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Workflow extends MY_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('workflow_m');
		$this->user_id = $this->session->userdata('user_id');
		$this->user_name = $this->session->userdata('user_name');
	}
	
	//流程新建视图
	public function flow_new(){
		$this->load->view('workflow/new');
	}
	
	//流程新建提交数据
	public function _flow_new_post(){
		$arrdata=$this->input->post();
		$data = $this->workflow_m->workflow_add_post($arrdata);
		if($data){
			$arr = $this->workflow_m->get_workflow_row_data($data,'flow_run.run_id,ps_id,run_title');
			$jsonarr = array("success"=>true,"id"=>$arr->run_id,"ps_id"=>$arr->ps_id,'title'=>$arr->run_title);
			echo json_encode($jsonarr);
		}else{
			$this->rejsonsuccess($data,"新建工作流失败，请联系管理员");	
		}
	}
	
	//流程列表视图
	public function workflow_list(){
		$this->load->view('workflow/flow_list');
	}
	
	//流程列表数据
	public function _workflow_data_post(){//收件箱JSON数据
		if($this->acl->check_privilege('workflow_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$rowdata = '';
			$data = $this->workflow_m->getlistdata($this->input->post());
			if(!empty($data)){
				foreach ($data as $v){
					$ps_num = $this->workflow_m->workflow_flowchart($v->run_id,1);
					$rowdata[] = array("id"=>$v->run_id,"ps_flag"=>$v->ps_flag,"title"=>$v->run_title,"begin_user"=>$v->begin_username,"ps_id"=>$v->ps_id,"ps_num"=>$ps_num,"begin_datetime"=>$v->begin_datetime);
				}
			}
			$data = array("total"=>count($data),'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}
	
	//流程办理视图
	function _flow_transact_post(){
		$data = $this->input->post(NULL, TRUE);
		$data['contentview'] = $this->workflow_m->contentbox($data['id'],$data['ps_id']);
		if($data['contentview']){
			//if($f === 0){
				if($data['is_v'] == 1){
					$data['attlist'] = $this->attlist('workflow',$data['contentview'][0]->attachment_id,$data['contentview'][0]->attachment_name,$data['contentview'][0]->size,$data['id'],$data['ps_id']);
					$this->load->view('workflow/flow_transact',$data);
				}else{
					echo $this->attlist('workflow',$data['contentview'][0]->attachment_id,$data['contentview'][0]->attachment_name,$data['contentview'][0]->size,$data['id'],$data['ps_id']);
				}

			//}else{
				//print_r($data['incontentview']);
			//	echo json_encode(array("success"=>true,"data"=>$data['contentview'][0]));
			//}
		}else{
			echo "<script>var tab = $('#main-tab').tabs('getSelected');var index = $('#main-tab').tabs('getTabIndex',tab);$('#main-tab').tabs('close',index);$.messager.alert('错误','没有找到该条工作，可能已被删除！','error');</script>";
		}
	}
	
	
	/*--工作流流程步骤视图---*/
	public function flow_chart(){
		$run_id=$this->uri->segment(3, 0);
		$data['m']=$this->uri->segment(4, 0);
		if($data['m'] == 0){
			$data['workflow_row_data'] = $this->workflow_m->get_workflow_row_data($run_id,'flow_run.run_id,run_title');
		}
		$data['flowchart'] = $this->workflow_m->workflow_flowchart($run_id);
		//print_r($data['flowchart']);
		$this->load->view('workflow/flow_chart',$data);

	}
	
	/*-上传附件-*/
	function _flow_att_update_post(){
		$data=$this->input->post();
		$data = $this->workflow_m->upfile($data);
		$this->rejsonsuccess($data,"上传文件失败，请联系管理员");		
	}
	
	//删除附件
	function _flow_att_del_post(){

		$data = $this->input->post();
		$data = $this->workflow_m->del_att($data);
		$this->rejsonsuccess($data,"删除附件失败，请联系管理员");	
	}

	//转交下一步视图
	function flow_next(){
		$data['run_id'] = $this->uri->segment(3, 0);
		$data['ps_id'] = $this->uri->segment(4, 0);
		$data['run_title'] = $this->uri->segment(5, 0);
		$data['this_username'] = $this->user_id;
		$this->load->view('workflow/flow_next',$data);
	}
	
	//转交下一步POST提交
	function _flow_next_post(){
		$data = $this->input->post();
		$data = $this->workflow_m->next_handed_post($data);
		$this->rejsonsuccess($data,"转交下一步失败，请联系管理员");	
	}
	
	//结束流程
	function _flow_end_post(){
		$data = $this->input->post();
		$data = $this->workflow_m->workflow_flow_end($data);
		$this->rejsonsuccess($data,"结束流程失败，请联系管理员");	
	}
	
	//删除流程
	function _flow_del_post(){
		$data = $this->input->post();
		$data = $this->workflow_m->workflow_flow_del($data);
		$this->rejsonsuccess($data,"结束流程失败，请联系管理员");	
	}
	
	//收回流程
	function _flow_back_post(){
		$data = $this->input->post();
		$data = $this->workflow_m->workflow_flow_back($data);
		$this->rejsonsuccess($data,"收回流程失败，请联系管理员");	
	}

	function advanced_search(){//高级搜索功能视图页
		$m = $this->uri->segment(3, 0);
		$t = '工作流';
		$data['v'] = array('m'=>$m,'t'=>$t);
		$this->load->view('workflow/flow_search',$data);
	}
	
	//查看流程内容
	function flow_checkout($id,$ps_id){
		$id = $this->uri->segment(3, 0);
		$ps_id = $this->uri->segment(4, 0);

		$data['contentview'] = $this->workflow_m->contentbox($id,$ps_id);

		$data['attlist'] = $this->attlist('workflow',$data['contentview'][0]->attachment_id,$data['contentview'][0]->attachment_name,$data['contentview'][0]->size,$id,0);
		$this->load->view('workflow/flow_checkout',$data);
	}

}
?>